package com.amuse.amuse.service.impl;

import com.amuse.amuse.entity.*;
import com.amuse.amuse.mapper.campus_permissions.CampusPermissionsDao;
import com.amuse.amuse.service.CampusPermissionsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class CampusPermissionsServiceImpl implements CampusPermissionsService {

    @Autowired
    CampusPermissionsDao campusPermissionsDao;

    //    // 获取日志记录器
    private static final Logger logger = LoggerFactory.getLogger(log.class);
    // 获取当前时间
    LocalDateTime now = LocalDateTime.now();

    // 定义日期时间格式器
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss");
    // 格式化日期时间
    private String publishDate = now.format(formatter);




    /*
    * 用户以及管理员发布帖子
    * */
    @Override
    public Integer addPosts(String content, Integer postId,Integer studentId ,  String publishDate) {
        //自动生成帖子的PostId，随机生成6位数字并判断不唯一,给publishDate添加时间戳
        publishDate = this.publishDate;
        postId = (int) (Math.random() * 900000) + 100000;
        return campusPermissionsDao.addPosts(content,postId,studentId,publishDate);

    }

    /*
    * 用户及管理员删除发布帖子
    * */

    @Override
    public Integer deletePosts(Integer postId) {
        return campusPermissionsDao.deletePosts(postId);
    }

    /*
    * 用户及管理员修改帖子
    * */
    @Override
    public Integer updatePosts(String content, String publishDate, Integer postId) {
        publishDate = this.publishDate;
        return campusPermissionsDao.updatePosts(content,publishDate,postId);
    }

    /*
    * 用户及管理员查看自己所发帖子
    * */
    @Override
    public PageBean<campusCirclePosts> findPostsByStudentId(Integer pageNum, Integer pageSize, Integer studentId) {
        PageBean<campusCirclePosts> campusCirclePostsPageBean = new PageBean<>();
        PageHelper.startPage(pageNum,pageSize);
        List<campusCirclePosts> postsByStudentId = campusPermissionsDao.findPostsByStudentId(studentId);
        Page<campusCirclePosts> page = (Page<campusCirclePosts>) postsByStudentId;
        campusCirclePostsPageBean.setItems(page.getResult());
        campusCirclePostsPageBean.setTotal(page.getTotal());
        return campusCirclePostsPageBean;
    }

    /*
    * 点击查看帖子下面的评论
    * */
    @Override
    public PageBean<postComments> findCommentsByPostId(Integer pageNum, Integer pageSize, Integer postId) {
        PageBean<postComments> postCommentsPageBean = new PageBean<>();
        PageHelper.startPage(pageNum,pageSize);
        List<postComments> postComments = campusPermissionsDao.findCommentsByPostId(postId);
        Page<postComments> page = (Page<postComments>) postComments;
        postCommentsPageBean.setItems(page.getResult());
        postCommentsPageBean.setTotal(page.getTotal());
        return postCommentsPageBean;
    }

    /*
    * 查看所有帖子及评论数和点赞数
    * */

    @Override
    public PageBean<campusCirclePosts> findAllPosts(Integer pageNum, Integer pageSize) {
        PageBean<campusCirclePosts> campusCirclePostsPageBean = new PageBean<>();
        PageHelper.startPage(pageNum,pageSize);
        List<campusCirclePosts> campusCirclePosts = campusPermissionsDao.findAllPosts();
        Page<campusCirclePosts> page = (Page<campusCirclePosts>) campusCirclePosts;
        campusCirclePostsPageBean.setItems(page.getResult());
        campusCirclePostsPageBean.setTotal(page.getTotal());
        return campusCirclePostsPageBean;
    }
    /*
    * 学生发表评论
    * */
    @Override
    public Integer addComments(String content, Integer commentId, Integer postId, Integer studentId, String publishDate) {
        publishDate = this.publishDate;
        //自动生成评论的CommentId，随机生成6位数字并判断不唯一,给publishDate添加时间戳
        commentId = (int) (Math.random() * 900000) + 100000;
//        postId为发表者点击的帖子的PostId，即从前端传过来的参数
        return campusPermissionsDao.addComments(content,commentId,postId,studentId,publishDate);
    }

    /*
    * 用户点赞
    * */

    @Override
    public Integer addLikes(Integer likeId, Integer postId, Integer studentId, Boolean status) {
        likeId = (int) (Math.random() * 900000) + 100000;
//        判断用户是否已经给帖子点赞，如果已经点赞则取消点赞，如果未点赞，则添加点赞
        List<postLikes> likesByStudentId = campusPermissionsDao.findLikesByStudentId(studentId);
        for (postLikes postLike : likesByStudentId) {
            if (postLike.getPostId() == postId) {
                logger.info("用户取消点赞");
                return campusPermissionsDao.deleteLikes(postLike.getPostId(), studentId);
            }
        }
        return campusPermissionsDao.addLikes(likeId,postId,studentId,status);


    }
    /*
    * 学生删除自己的评论
    * */
    @Override
    public Integer deleteComments(Integer commentId) {
        return campusPermissionsDao.deleteComments(commentId);
    }

    /*
    * 学生取消点赞
    * */
    @Override
    public Integer deleteLikes(Integer postId, Integer studentId) {
        return null;
    }

    /*
    * 根据学号查询自己在帖子下所有的评论
    * */

    @Override
    public PageBean<postComments> findCommentsByStudentId(Integer pageNum, Integer pageSize, Integer studentId) {
        PageBean<postComments> postCommentsPageBean = new PageBean<>();
        PageHelper.startPage(pageNum,pageSize);
        List<postComments> postComments = campusPermissionsDao.findCommentsByStudentId(studentId);
        Page<postComments> page = (Page<postComments>) postComments;
        postCommentsPageBean.setItems(page.getResult());
        postCommentsPageBean.setTotal(page.getTotal());
        return postCommentsPageBean;

    }
    /*
    *  根据学号查询自己在帖子下所有的点赞
    * */
    @Override
    public PageBean<postLikes> findLikesByStudentId(Integer pageNum, Integer pageSize, Integer studentId) {
        PageBean<postLikes> postLikesPageBean = new PageBean<>();
        PageHelper.startPage(pageNum,pageSize);
        List<postLikes> postLikes = campusPermissionsDao.findLikesByStudentId(studentId);
        Page<postLikes> page = (Page<postLikes>) postLikes;
        postLikesPageBean.setItems(page.getResult());
        postLikesPageBean.setTotal(page.getTotal());
        return postLikesPageBean;
    }
}
